var app = getApp();
var util = require('../../utils/util');
Page({
  data: {
    img_url: app.globalData.SiteDomain,
    filterId: 1,
    address: '定位中…',
    banners: [{
      id: 1,
      img: 'https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1534049339473&di=21b3cc346532c24011a61fc138ec554e&imgtype=0&src=http%3A%2F%2Fp2.diaoyu123.com%2FM01%2FCF%2FDD%2FCgAAilbFP72APdDvAANnFy8v6lU697.jpg',
      url: '',
      name: '旅拍啦'
    }],
    fishList: [],
    datePickerData: [],
    selDateIdx: -1,
    winHeight: 0, //窗口高度
    page: 1
  },
  onLoad: function() {
    var that = this;
    //  高度自适应
    wx.getSystemInfo({
      success: function(res) {
        var clientHeight = res.windowHeight,
          clientWidth = res.windowWidth,
          rpxR = 750 / clientWidth;
        var calc = clientHeight * rpxR - 180;

        that.setData({
          winHeight: calc
        });
      }
    });
    app.getUserInfo(function(res) { //如果已授权，则登录或注册
      if (res != null) {
        app.login();
      }
    });
    that.initDatePicker();
    that.loadfishing();
  },
  onShow: function() {},
  onScroll: function(e) {
    if (e.detail.scrollTop > 100 && !this.data.scrollDown) {
      this.setData({
        scrollDown: true
      });
    } else if (e.detail.scrollTop < 100 && this.data.scrollDown) {
      this.setData({
        scrollDown: false
      });
    }
  },
  changeDate: function(e) {
    //console.log(e);
    var that = this;
    let _idx = e.currentTarget.dataset.idx;
    this.setData({
      selDateIdx: _idx
    })
    if (_idx == -1) {
      that.loadfishing();
    } else {
      var selDate = that.data.datePickerData[_idx];
      console.log(selDate);
      var postData = {
        bgtime: selDate.year + "-" + selDate.month + "-" + selDate.day
      };
      that.loadfishing(postData);
    }
  },
  initDatePicker: function() {
    var dateArry = util.getDates(7);
    dateArry[0].week = "今天";
    dateArry[1].week = "明天";
    this.setData({
      datePickerData: dateArry
    })
  },
  loadfishing: function(obj) {
    var list = [];
    var that = this;
    var _token = app.globalData.token;
    var _page = that.data.page;
    wx.showLoading({
      title: '加载中...',
    })
    var postData = {
      token: _token,
      page: _page
    }
    if (obj) {
      postData = Object.assign(postData, obj);
    }
    wx.request({
      url: app.globalData.Domain + '/fish/list',
      data: postData,
      success: function(res) {
        if (res.data.code == 0) {
          var list = res.data.data;
          for (var i = 0; i < list.length; i++) {
            var item = list[i];
            item.distance_fix = item.distance.toFixed(1);
          }
          that.setData({
            fishList: list
          })
        } else {

          wx.showToast({
            title: res.data.msg,
          })
        }
      },
      complete: function() {
        wx.hideLoading();
        wx.stopPullDownRefresh();
      }
    })

  },
  toLocation: function(e) {
    wx.navigateTo({
      url: 'location',
      success: function(res) {
        // success
      },
      fail: function() {
        // fail
      },
      complete: function() {
        // complete
      }
    })
  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {
    var that = this;
    that.setData({
      page: 1
    })
    that.loadfishing();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  },
  codeTap: function() {
    wx.scanCode({
      success: (res) => {
        wx.navigateTo({
          url: '../detail/detail',
          success: function(res) {
            wx.showToast({
              title: '扫码成功',
              icon: 'success',
              duration: 1000
            })
          },
          fail: function() {
            // fail
          },
          complete: function() {
            // complete
          }
        })
      },
      fail: (re) => {
        wx.showModal({
          content: '扫码失败,该扫码只支持扫描小程序二维码',
          success: function(res) {
            if (res.confirm) {
              wx.navigateBack({
                delta: 1, // 回退前 delta(默认为1) 页面
                success: function(res) {
                  // success
                },
                fail: function() {
                  // fail
                },
                complete: function() {
                  // complete
                }
              })
            }
          }
        })

      }
    })
  }
})

;